package top.itaso.lib.widget;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;


/**
 * @Description:
 * @Author itsdf07
 * @E-Mail 923255742@qq.com
 * @Gitee https://gitee.com/itsdf07
 * @Date 2022/9/24
 */
public class RelationFindView extends androidx.appcompat.widget.AppCompatEditText {
    private final String TAG = this.getClass().getSimpleName();
    private ArrayList<Node> allNodes = new ArrayList<>();
    private ArrayList<Node> findNodes = new ArrayList<>();

    public RelationFindView(@NonNull Context context) {
        this(context, null);
    }

    public RelationFindView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        addTextChangedListener(inputChanged);
    }


    public void updateNodes(ArrayList<Node> newNodes) {
        if (newNodes == null) {
            return;
        }
        allNodes.clear();
        allNodes.addAll(newNodes);
    }

    /**
     * 从 allNodes 中过滤出关键值
     *
     * @param keyValue
     */
    private void filterFromAllNodes(String keyValue) {
        if (keyValue == null) {
            return;
        }
        findNodes.clear();
        for (Node node : allNodes) {
            if (node.name.contains(keyValue)) {
                findNodes.add(node);
            }
        }
    }

    /**
     * 从 findNodes 中过滤出关键值，并更新到 findNodes
     *
     * @param keyValue
     */
    private void filterFromFindNodes(String keyValue) {
        if (keyValue == null) {
            return;
        }
        ArrayList<Node> temp = new ArrayList<>();

        for (Node node : findNodes) {
            if (node.name.contains(keyValue)) {
                findNodes.add(node);
            }
        }
        findNodes.clear();
        findNodes.addAll(temp);
    }

    private TextWatcher inputChanged = new TextWatcher() {
        private String beforeText = "";

        @Override
        public void beforeTextChanged(CharSequence beforeChangeText, int start, int count, int after) {
            Log.v(TAG, "beforeTextChanged: >>>>>>start:" + start + ",after" + after + ",count:" + count + ",beforeChangeText:" + beforeChangeText);
            beforeText = beforeChangeText.toString();
        }

        @Override
        public void onTextChanged(CharSequence changeText, int start, int before, int count) {
            Log.v(TAG, "beforeTextChanged: >>>>>>start:" + start + ",before" + before + ",count:" + count + ",changeText:" + changeText);
        }

        @Override
        public void afterTextChanged(Editable resultText) {
            String afterChangeText = resultText.toString();
            Log.v(TAG, "afterTextChanged: >>>>>>afterChangeText=" + afterChangeText);
            if (afterChangeText.length() > beforeText.length()) {
                filterFromFindNodes(afterChangeText);
            } else if (afterChangeText.length() < beforeText.length()) {
                filterFromAllNodes(afterChangeText);
            } else {

            }
        }
    };


    public static class Node<T> {
        /**
         * 当前节点id
         */
        private String id;

        /**
         * 父节点id
         */
        private String pid;

        /**
         * 节点数据实体类
         */
        private T data;

        /**
         * 下一级的子Node
         */
        private List<Node> children = new ArrayList<>();


        /**
         * 节点名称
         */
        private String name;

        /**
         * 当前的级别
         */
        private int level;

        /**
         * 父Node
         */
        private Node parentNode;


        public Node() {
        }

        public Node(String id, String pid, T data, List<Node> children, String name,
                    int level, Node parentNode) {
            this.id = id;
            this.pid = pid;
            this.data = data;
            this.children = children;
            this.name = name;
            this.level = level;
            this.parentNode = parentNode;
        }

        public Node(String id, String pid, String name) {
            this.id = id;
            this.pid = pid;
            this.name = name;
        }

        public Node(String id, String pid, String name, T data) {
            this.id = id;
            this.pid = pid;
            this.data = data;
            this.name = name;
        }

        public Node(String id, String pid, String name, List<Node> children) {
            this.id = id;
            this.pid = pid;
            this.children = children;
            this.name = name;
        }

        /**
         * @return 当前是否为根节点
         */
        public boolean isRootNode() {
            return parentNode == null;
        }


        /**
         * @return 当前是否叶子节点，即最后一级
         */
        public boolean isLeaf() {
            return children == null || children.isEmpty();
        }

        /**
         * @return 获取当前节点的级别
         */
        public int getLevel() {
            return parentNode == null ? 0 : parentNode.getLevel() + 1;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getPid() {
            return pid;
        }

        public void setPid(String pid) {
            this.pid = pid;
        }

        public T getData() {
            return data;
        }

        public void setData(T data) {
            this.data = data;
        }

        public List<Node> getChildren() {
            return children;
        }

        public void setChildren(List<Node> children) {
            this.children = children;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public void setLevel(int level) {
            this.level = level;
        }

        public Node getParentNode() {
            return parentNode;
        }

        public void setParentNode(Node parentNode) {
            this.parentNode = parentNode;
        }

        public static class NodeData {

            private String name;

            public NodeData() {
            }

            public NodeData(String name) {
                this.name = name;
            }

            public String getName() {
                return name;
            }

            public void setName(String name) {
                this.name = name;
            }
        }
    }

}
